/*! 
 * (c) 2008 Kazuki UCHIDA
 * MIT License
 */
Dialog = (function(){
    var overlay = null;
    var container = null;
    var header = null;
    var title = null;
    var close = null;
    var clear = null;
    var content = null;
    var init = function(){
        if (!overlay) {
            overlay = document.createElement('div');
            overlay.setAttribute('class', 'dialog_overlay');

            container = document.createElement('div');
            container.setAttribute('class', 'dialog_container');

            header = document.createElement('div');
            header.setAttribute('class', 'dialog_header');

            title = document.createElement('div');
            title.setAttribute('class', 'dialog_title');

            close = document.createElement('div');
            close.setAttribute('class', 'dialog_close');
            close.innerHTML = '<a href="javascript:void(0)" onclick="Dialog.close()">CLOSE</a>';

            clear = document.createElement('div');
            clear.style.clear = 'both';

            content = document.createElement('div');
            content.setAttribute('class', 'dialog_content');
        }
    }
    var setTitle = function(str){
        title.innerHTML = '<span>' + str + '</span>';
    };
    var setContent = function(func){
        func(content);
    };
    var setSize = function(w, h){
        with(container.style) {
            width = w + 'px';
            height = h + 'px';
            top = '50%';
            left = '50%';
            marginTop = (-1 * h / 2) + 'px';
            marginLeft = (-1 * w / 2) + 'px';
        }
    };
    var show = function(){
        header.appendChild(title);
        header.appendChild(close);
        header.appendChild(clear);
        container.appendChild(header);
        container.appendChild(content);
        document.body.appendChild(overlay);
        document.body.appendChild(container);
    };
    var hide = function(){
        if (overlay) document.body.removeChild(overlay);
        if (container) document.body.removeChild(container);
    };
    var extend = function(s, p) {
        for (var i in p) {
            s[i] = p[i];
        }
    };
    return {
        open: function(p){
            s = { width: 200,
                  height: 200,
                  title: 'Title',
                  content: function(dom){ dom.innerHTML = 'Content'; } };
            extend(s, p);
            init();
            setTitle(s.title);
            setContent(s.content);
            setSize(s.width, s.height);
            show();
        },
        close: function(){
            hide();
        },
    };
})();
